package com.lw.obj;

import com.lw.GameWin;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import java.awt.*;

/**
 * 所有游戏物体的父类
 */
@Data
@AllArgsConstructor //全部参数的构造方法
@NoArgsConstructor //无参构造方法
public class GameObj {

    Image img;//物体图片

    //坐标
    int x;
    int y;

    //宽高 30是因为 GameWin每个小格子宽高是30=600/20
    int width = 30;
    int height = 30;

    GameWin gameWin;//窗口类

    // 创建一个媒体跟踪器
    MediaTracker tracker;

    public GameObj(Image img, int x, int y, GameWin gameWin) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.gameWin = gameWin;

        if(gameWin!=null){
            tracker = new MediaTracker(gameWin);

            tracker.addImage(img, 0);

            // 等待图像加载
            try {
                tracker.waitForAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 绘制自身
     */
    public void paintSelf(Graphics g){
        g.drawImage(img,x,y,null);
    }
}
